package apps.softtech.fastmarket;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.gms.gcm.GoogleCloudMessaging;


import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import apps.softtech.Utility.ApplicationConstants;
import apps.softtech.fastmarket.model.ChatLog;
import apps.softtech.fastmarket.model.DatabaseHandler;

public class ServiceGoogleCM extends IntentService{

	public static final int NOTIFICATION_ID = 1;
    NotificationCompat.Builder builder;
	String msg=null;
	int idFromUser;
	int idToUser;
	int type;
	public ServiceGoogleCM() {
		super("202517753016");
	}

	private Handler handler;
	@Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        handler = new Handler();
    }
	@Override
	protected void onHandleIntent(Intent intent) {
		Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);
        msg = extras.getString("price");
        Log.v("respone:", msg);
        try {
			JSONObject mainObject = new JSONObject(extras.getString("price"));
			msg = mainObject.getString("data");
			idFromUser = mainObject.getInt("from");
			idToUser = mainObject.getInt("to");
			type = mainObject.getInt("type");
		} catch (JSONException e) {
			e.printStackTrace();
		}
//        showToast();//
        if(msg.toString().trim().equals("")){//
        	return;
        }else{
//        	if(idFromUser!=-1){
        	//
        	//save log
        	//
        	DatabaseHandler dbh = new DatabaseHandler(this);
        	dbh.insertChatLog(new ChatLog(msg, toDay(), idFromUser, idToUser, type));
//        	Intent inChat = new Intent(this,ServiceShowChatHead.class);
//        	startService(inChat);
        	Log.v("respone:", msg);
        	//send broadcast
        	Intent inSend = new Intent(ApplicationConstants.RECEIVER_SMS);
        	inSend.putExtra("idfromuser", idFromUser);
        	inSend.putExtra("data", msg);
        	inSend.putExtra("idtouser", idToUser);
        	inSend.putExtra("type", type);
        	sendBroadcast(inSend);
//        }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        ReveiveGoogleCM.completeWakefulIntent(intent);
	}
	/**
	 * show toast
	 */
	 public void showToast(){
	        handler.post(new Runnable() {
	            public void run() {
	                Toast.makeText(getApplicationContext(),"thong tin:"+msg , Toast.LENGTH_LONG).show();
	            }
	         });

	    }
	 /**
		 * Today
		 * 
		 * @return
		 */
		private String toDay() {
			Calendar c = Calendar.getInstance();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String formattedDate = df.format(c.getTime());
			return formattedDate;
		}
	

}
